package com.liang.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/19
 * Time: 15:48
 * Description: 发布确认 高级
 */
@Configuration
public class ConfirmConfig {



    //交换机
    public static final String CONFIRM_EXCHANGE_NAME = "confirm.exchange";

    //队列
    public static final String CONFIRM_QUEUE_NAME = "confirm_queue";

    //routingkey
    public static final String CONFIRM_ROUTING_KEY = "key1";

    //备份交换机
    public static final String BACKUP_EXCHANGE_NAME = "backup_exchange";

    //备份队列
    public static final String BACKUP_QUEUE_NAME = "backup_queue";
    //报警队列
    public static final String WARN_QUEUE_NAME = "warn_queue";


    @Bean
    public DirectExchange directExchange(){

        //使用了备份交换机
       return ExchangeBuilder.directExchange(CONFIRM_EXCHANGE_NAME).durable(true).alternate(BACKUP_EXCHANGE_NAME).build();

    }

    @Bean
    public Queue queue(){

        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();

    }

    @Bean
    public Binding bindingx(@Qualifier("directExchange") DirectExchange directExchange,@Qualifier("queue") Queue queue){

        return BindingBuilder.bind(queue).to(directExchange).with("key1");

    }


    /**
     * 备份交换机创建
     */

    @Bean
    public FanoutExchange backupDirectExchange(){

        return new FanoutExchange(BACKUP_EXCHANGE_NAME);

    }

    /**
     * 备份队列
     */

    @Bean
    public Queue backupQueue(){
        return QueueBuilder.durable(BACKUP_QUEUE_NAME).build();
    }


    @Bean
    public Queue warnQueue(){
        return QueueBuilder.durable(WARN_QUEUE_NAME).build();
    }

    /**
     * 备份 报警队列绑定备份交换机
     */

    @Bean
    public Binding backupQBindingBackExchange(@Qualifier("backupQueue") Queue backQueue,@Qualifier("backupDirectExchange") FanoutExchange backupExchange){

        return BindingBuilder.bind(backQueue).to(backupExchange);

    }

    @Bean
    public Binding warnQBindingBackExchange(@Qualifier("warnQueue") Queue warnQueue,@Qualifier("backupDirectExchange") FanoutExchange backupExchange){

        return BindingBuilder.bind(warnQueue).to(backupExchange);

    }

}